#!/bin/bash
# tmux
#args_ip=$(ip -o route get to 8.8.8.8 | sed -n 's/.*src \([0-9.]\+\).*/\1/p')

#args_pwd='wowmanage.cn'
#args_name='AzerothCore'
args_type=$1

if ((${#@} < 1)); then
  echo "参数不全，重新输入"
  exit 1
fi

sudo apt update && sudo apt install -y git unzip zip

#下载源码 可能要输入密码，放在第一位
cd /
if [ "$args_type" = 1 ]; then
    git clone https://gitee.com/53957105/azerothcore-wotlk.git azerothcore
    cd /azerothcore/modules
    unzip -q modules.zip
elif [ "$args_type" = 2 ]; then
    git clone https://gitee.com/53957105/AzerothCore-wotlk-with-NPCBots.git --branch cn azerothcore
    cd /azerothcore/modules
    sh getMod.sh
else
    git clone https://gitee.com/53957105/azerothcore-wotlk-github.git --branch cn azerothcore
    cd /azerothcore/modules
    sh getMod.sh
fi

timedatectl set-timezone Asia/Shanghai

# 开放3306端口
ufw allow 22
ufw allow 3306
ufw allow 8085
ufw allow 3724
ufw allow 7878
ufw reload



#编译
chmod +x /azerothcore/acore.sh
chmod +x /azerothcore/deps/jsonpath/JSONPath.sh
cp /azerothcore/conf/dist/config.sh /azerothcore/conf/
/azerothcore/acore.sh install-deps
/azerothcore/acore.sh compiler all

# 设置允许远程登录
echo '[mysqld]' >>  /etc/mysql/my.cnf
echo 'bind-address=0.0.0.0' >>  /etc/mysql/my.cnf
echo 'default_authentication_plugin=mysql_native_password' >>  /etc/mysql/my.cnf
service mysql restart

#初始化数据库
mysql -uroot <<EOF
DROP USER IF EXISTS 'root'@'%';
CREATE USER 'root'@'%' IDENTIFIED BY 'wowmanage.cn' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0;
GRANT ALL PRIVILEGES ON * . * TO 'root'@'%';
DROP USER IF EXISTS 'acore'@'localhost';
CREATE USER 'acore'@'localhost' IDENTIFIED BY 'acore' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0;
GRANT ALL PRIVILEGES ON * . * TO 'acore'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EOF

cp /azerothcore/apps/db_assembler/conf.dist.sh /azerothcore/apps/db_assembler/config.sh
if [ "$args_type" = 1 ]; then
    bash "/azerothcore/apps/db_assembler/db_assembler.sh" import-all

	sed -i '/^Anticheat.Enabled = /c Anticheat.Enabled = 0' /azerothcore/env/dist/etc/Anticheat.conf.dist
	sed -i '/^Anticheat.EnabledOnGmAccounts = /c Anticheat.EnabledOnGmAccounts = 0' /azerothcore/env/dist/etc/Anticheat.conf.dist
    sed -i '/^CFBG.Enable/c CFBG.Enable = 0' /azerothcore/env/dist/etc/CFBG.conf.dist
    sed -i '/^LearnSpells.Enable/c LearnSpells.Enable = 0' /azerothcore/env/dist/etc/mod_learnspells.conf.dist
    sed -i '/^LearnSpells.Announce/c LearnSpells.Announce = 0' /azerothcore/env/dist/etc/mod_learnspells.conf.dist
else
    bash "/azerothcore/apps/db_assembler/db_assembler.sh" import-bases

    sed -i '/^Anticheat.Enabled = /c Anticheat.Enabled = 0' /azerothcore/env/dist/etc/modules/Anticheat.conf.dist
    sed -i '/^Anticheat.EnabledOnGmAccounts = /c Anticheat.EnabledOnGmAccounts = 0' /azerothcore/env/dist/etc/Anticheat.conf.dist
    sed -i '/^CFBG.Enable/c CFBG.Enable = 0' /azerothcore/env/dist/etc/modules/CFBG.conf.dist
    sed -i '/^LearnSpells.Enable/c LearnSpells.Enable = 0' /azerothcore/env/dist/etc/modules/mod_learnspells.conf.dist
    sed -i '/^LearnSpells.Announce/c LearnSpells.Announce = 0' /azerothcore/env/dist/etc/modules/mod_learnspells.conf.dist
    sed -i '/^ModBGItemReward.Enable/c ModBGItemReward.Enable = 0' /azerothcore/env/dist/etc/modules/mod-bg-item-reward.conf.dist
    sed -i '/^Skip.Deathknight.Starter.Announce.enable/c Skip.Deathknight.Starter.Announce.enable = 0' /azerothcore/env/dist/etc/modules/SkipDKModule.conf.dist
    sed -i '/^Skip.Deathknight.Starter.Enable/c Skip.Deathknight.Starter.Enable = 0' /azerothcore/env/dist/etc/modules/SkipDKModule.conf.dist
fi

# 设置默认值文件
sed -i '/^SOAP.Enabled/c SOAP.Enabled = 1' /azerothcore/env/dist/etc/worldserver.conf.dist
sed -i '/^SOAP.IP/c SOAP.IP = "0.0.0.0"' /azerothcore/env/dist/etc/worldserver.conf.dist



#打包
apt install -y zip
cd /azerothcore
zip -r -q -o "ac-$args_type.zip" apps/ conf/ data/ deps/ env/ modules/

# 下载data
cd ~
if [ "$args_type" = 1 ]; then
    /azerothcore/acore.sh client-data
else
    rm /root/data.zip
    wget http://hx.phasercn.com/client-data/releases/download/v16/data.zip
    cd /azerothcore/env/dist/bin
    unzip -q ~/data.zip
fi

#复制配置文件
cp /azerothcore/env/dist/etc/authserver.conf.dist /azerothcore/env/dist/etc/authserver.conf
cp /azerothcore/env/dist/etc/worldserver.conf.dist /azerothcore/env/dist/etc/worldserver.conf

#运行
tmux new-session -d -s auth-session
tmux new-session -d -s world-session
tmux send-keys -t auth-session '/azerothcore/env/dist/simple-restarter /azerothcore/env/dist/bin authserver' C-m
tmux send-keys -t world-session '/azerothcore/env/dist/simple-restarter /azerothcore/env/dist/bin worldserver' C-m
